//
//  ViewController.m
//  SLSlideImage
//
//  Created by 伟仔c on 15/4/6.
//  Copyright (c) 2015年 SL. All rights reserved.
//

#import "SLSlideImageViewController.h"

@interface SLSlideImageViewController () <UIScrollViewDelegate>

/** 幻灯滚动图  */
@property (nonatomic, weak) UIScrollView *slideScrollrtView;
/** 页码控制器 */
@property (nonatomic, weak) UIPageControl *pageControl;
/** 定时器 */
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation SLSlideImageViewController

#pragma mark -实例化一个图片轮播器
+ (instancetype)slideImageWithImageNames:(NSArray *)imageNames intervalTime:(NSInteger)intervalTime {
    
    // 1.实例化自己
    SLSlideImageViewController *playImageViewController = [[self alloc] init];
    
    // 2.存储图片名
    playImageViewController.imageNames = imageNames;
    
    // 3.存储间隔时间
    playImageViewController.intervalTime = intervalTime;
    
    // 3.返回自己
    return playImageViewController;
}

#pragma mark -视图即将显示
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    // 1.设置ScrollView
    [self setupScrollerView];
    
    // 2.设置PageControl
    [self setupPageControl];
    
    // 3.开启定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:_intervalTime target:self selector:@selector(rollSlide) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

#pragma mark -设置ScrollView
- (void)setupScrollerView {
    
    // 1.创建幻灯滚动ScrollView
    UIScrollView *slideScrollrtView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _slideScrollrtView = slideScrollrtView;
    
    // 2.设置幻灯滚动ScrollView
    _slideScrollrtView.contentOffset = CGPointZero;
    _slideScrollrtView.contentSize = CGSizeMake(_slideScrollrtView.frame.size.width * _imageNames.count, 0);
    _slideScrollrtView.pagingEnabled = YES;
    _slideScrollrtView.showsHorizontalScrollIndicator = NO;
    _slideScrollrtView.delegate = self;
    
    // 3.设置幻灯图片
    for (int i = 0; i < _imageNames.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        // 计算图片的位置
        CGFloat imgW = _slideScrollrtView.frame.size.width;
        CGFloat imgH = _slideScrollrtView.frame.size.height;
        CGFloat imgX = imgW * i;
        CGFloat imgY = 0;
        imageView.frame =CGRectMake(imgX, imgY, imgW, imgH);
        [_slideScrollrtView addSubview:imageView];
        
        // 拼接图片名
        NSString *imgName = _imageNames[i];
        imageView.image = [UIImage imageNamed:imgName];
    }
    
    // 4.添加到父视图
    [self.view addSubview:slideScrollrtView];
}

#pragma mark -设置PageControl
- (void)setupPageControl {
    
    // 1.创建PageControl
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    
    // 2.设置PageControl
    pageControl.numberOfPages = _slideScrollrtView.contentSize.width / CGRectGetWidth(_slideScrollrtView.frame);
    pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    pageControl.currentPage = 0;
    
    // 3.计算page的位置
    CGFloat pageW = CGRectGetWidth(_slideScrollrtView.frame) / 5;
    CGFloat pageH = 20;
    CGFloat pageX = (CGRectGetWidth(self.view.frame) - pageW) / 2;
    CGFloat pageY = CGRectGetHeight(_slideScrollrtView.frame) * 0.9;
    pageControl.frame = CGRectMake(pageX, pageY, pageW, pageH);
    
    // 4.添加到父视图
    _pageControl = pageControl;
    [self.view addSubview:pageControl];
}

#pragma mark -定时器.每轮间隔时间触发一次
- (void)rollSlide {
    
    // 1.拿到当前页码
    __block NSInteger index = _pageControl.currentPage;
    
    // 动画效果
    [UIView animateWithDuration:1 animations:^{
        
        // 2.页码加1
        if (index < _pageControl.numberOfPages) {
            _pageControl.currentPage = index + 1;
            CGFloat currentX = _slideScrollrtView.frame.size.width * (index+1);
            _slideScrollrtView.contentOffset = CGPointMake(currentX, 0);
        }
        
        // 3.页码到末尾，回到第一页
        if (index == _pageControl.numberOfPages-1) {
            _pageControl.currentPage = 0;
            index = _pageControl.currentPage;
            CGFloat currentX = _slideScrollrtView.frame.size.width * (index);
            _slideScrollrtView.contentOffset = CGPointMake(currentX, 0);
        }
    }];
}

#pragma mark -开启定时器
- (void)startTimer {
    
    // 1.定时器开启状态，2秒执行一次
    _timer = [NSTimer scheduledTimerWithTimeInterval:_intervalTime target:self selector:@selector(rollSlide) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}

#pragma mark -停止定时器
- (void)stopTimer {
    
    // 1.停止定时器
    [_timer invalidate];
    _timer = nil;
}

#pragma mark -ScrollView代理方法，触摸过程中一直调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    // 1.设置页面指示器
    CGFloat scrollViewW = CGRectGetWidth(_slideScrollrtView.frame);
    int page = (_slideScrollrtView.contentOffset.x + scrollViewW * 0.5) / scrollViewW;
    _pageControl.currentPage = page;
}

#pragma mark -ScrollView代理方法，触摸开始时调用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    // 1.停止滚动
    [self stopTimer];
}

#pragma mark -ScrollView代理方法，触摸结束时调用
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
    // 1.开启滚动
    [self startTimer];
}

@end
